"""

@author: lingw
@email: gw.lin@hzgosun.com
@file: viewUserRoleRoleManagementTest.py
@time: 2019-5-22 下午 2:23

这一行开始写关于本文件的说明与解释

"""

import unittest

from time import sleep

from selenium.webdriver.common.keys import Keys

from common.decorators import mkdir_classname,log4gs
from common.functions import save_screenshot
from common.settings import TAG
from common.vue.select import DropDownSelect, OperateSelect
from pages.viewUserRoleManagementPage import ViewUserRoleManagementPage

@log4gs
@mkdir_classname(TAG)
class ViewUserRoleManagementTest(unittest.TestCase):
    """前台角色管理页面功能测试"""
    @classmethod
    def setUpClass(cls):
        cls.log.info('=' * 30 + "开始前台角色管理功能测试({})".format(__class__.__name__) + '=' * 30)
        cls.page = ViewUserRoleManagementPage('/UserManage/ViewUserRoleManage')

    @classmethod
    def tearDownClass(cls):
        cls.log.info('=' * 30 + "结束前台角色管理功能测试({})".format(__class__.__name__) + '=' * 30)
        cls.page.driver.quit()

    def setUp(self):
        self.log.info(">>>>>>开始执行用例:{} {}".format(self.id(), self.shortDescription()))

    def tearDown(self):
        self.log.info("<<<<<<结束执行用例:{} {}".format(self.id(), self.shortDescription()))

    @unittest.skip
    def test_1_turn_page(self):
        """翻页功能"""
        self.log.debug("当前共有：" + self.page.turnPage.total_page +"页数据" )
        self.log.debug("当前处于第：" + self.page.turnPage.current_page + "页数据")
        self.assertEqual(self.page.turnPage.current_page, '1')
        sleep(1)
        try:
            self.assertNotEqual(self.page.turnPage.total_page, 1, msg="页数过少，无法进行翻页测试")
            self.page.turnPage.next_page.click()
            self.assertEqual(self.page.turnPage.current_page, '2')

            sleep(1)
            self.page.turnPage.next_page.click()
            self.assertEqual(self.page.turnPage.current_page, '3')

            sleep(1)
            self.page.turnPage.prev_page.click()
            self.assertEqual(self.page.turnPage.current_page, '2')

            sleep(1)
            self.page.turnPage.input.clear()
            self.page.turnPage.input.send_keys(6)
            self.page.turnPage.input.send_keys(Keys.ENTER)
            self.assertEqual(self.page.turnPage.current_page, '6')
        except Exception:
            self.log.error("当前模块最大为1页，无法进行翻页测试", exc_info=True)


    # @unittest.skip
    def test_2_add_foreground_role(self):
        """增加前台角色
        用例连接：http://181.181.0.216:8786/tc/detail/74-4910/
        """
        self.page.table.add_btn.click()
        sleep(2)
        self.page.addViewUserRole.add_role_name.send_keys("测试前台角色管理")
        self.page.addViewUserRole.page_role.select("社会治理智治网数据接入平台")
        sleep(2)
        self.page.addViewUserRole.confirm.click()
        sleep(1)
        self.page.searchTools.search_role_name.send_keys("测试前台角色管理")
        sleep(1)
        self.page.searchTools.click_btn.click()
        result = self.page.table.get_row("测试前台角色管理")
        sleep(1)
        try:
            self.assertTrue(result, msg="无法查找到目标：测试前台角色管理")
            self.log.info("用例'{}'执行成功".format(self.id()))
        except Exception:
            self.log.error("用例'{}'执行失败".format(self.id()), exc_info=True)
        self.page.searchTools.search_role_name.clear()

    # @unittest.skip
    def test_3_search_foreground_role(self):
        """查询前台用户角色测试
        http://181.181.0.216:8786/tc/detail/74-4924/
        """

        self.page.searchTools.search_role_name.send_keys("测试前台角色管理")
        self.page.searchTools.click_btn.click()
        result = self.page.table.get_row("测试前台角色管理")
        sleep(1)

        try:
            self.assertTrue(result, msg="没有找到前台角色：测试前台角色管理")
            self.log.info("用例‘{}’执行成功".format(self.id()))
        except Exception:
            self.log.error("用例‘{}’执行失败".format(self.id()), exc_info=True)
        self.page.searchTools.search_role_name.clear()

    # @unittest.skip
    def test_4_modify_foreground_role(self):
        """修改前台用户角色
        http://181.181.0.216:8786/tc/detail/74-4931/
        """

        self.page.searchTools.search_role_name.send_keys("测试前台角色管理")
        self.page.searchTools.click_btn.click()
        sleep(2)

        search_result = self.page.table.get_row("测试前台角色管理")
        sleep(1)
        OperateSelect(search_result, "修改")
        sleep(2)
        self.page.modifyViewUserRole.add_role_name.clear()
        self.page.modifyViewUserRole.add_role_name.send_keys("测试修改前台角色管理")
        self.page.modifyViewUserRole.page_role.select("智慧物业平台")
        self.page.modifyViewUserRole.confirm.click()
        sleep(2)

        self.page.searchTools.search_role_name.clear()
        self.page.searchTools.search_role_name.send_keys("测试修改前台角色管理")
        self.page.searchTools.click_btn.click()

        modify_search_result = self.page.table.get_row("测试修改前台角色管理")

        try:
            self.assertTrue(modify_search_result, msg="未查询到修改后前台用户角色")
            self.log.info("修改前台用户角色用例执行成功")
        except Exception:
            self.log.error("修改前台用户角色用例执行失败", exc_info=True)
        self.page.searchTools.search_role_name.clear()

    # @unittest.skip
    def test_5_block_up_start_foreground_role(self):
        """停/启用前台账号
        http://181.181.0.216:8786/tc/detail/74-4926/
        http://181.181.0.216:8786/tc/detail/74-4927/
        """

        self.page.searchTools.search_role_name.send_keys("测试修改前台角色管理")
        self.page.searchTools.click_btn.click()
        sleep(2)

        search_result = self.page.table.get_row("测试修改前台角色管理")
        OperateSelect(search_result, "停用")
        sleep(2)

        block_up_result = self.page.table.get_row("停用")
        try:
            self.assertTrue(block_up_result, msg="前台账号停用失败")
            self.log.info("停用前台角色执行成功")
        except Exception:
            self.log.error("停用前台角色执行失败", exc_info=True)
        sleep(2)

        OperateSelect(search_result, "启用")
        sleep(2)

        start_up_result = self.page.table.get_row("启用")
        try:
            self.assertTrue(start_up_result, msg="前台账号启用失败")
            self.log.info("启用前台角色执行成功")
        except Exception:
            self.log.error("启用前台角色执行失败", exc_info=True)
        sleep(2)


if __name__ == '__main__':
    unittest.main()






